package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaFile;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaFileDao {

    /**
     * 查询
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Results(id = "pcbaFileResultMap", value = {
            @Result(column = "pcba_file_id", property = "pcbaFileId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_resource_id", property = "pcbaDeviceResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_file_type", property = "pcbaFileType", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_file_path", property = "pcbaFilePath", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_file_name", property = "pcbaFileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "file_group_id", property = "fileGroupId", jdbcType = JdbcType.INTEGER),
            @Result(column = "creator_id", property = "creatorId", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_company_id", property = "pcbaCompanyId", jdbcType = JdbcType.INTEGER),
            @Result(column = "submitter", property = "submitter", jdbcType = JdbcType.INTEGER),
            @Result(column = "auditors", property = "auditors", jdbcType = JdbcType.VARCHAR),
            @Result(column = "case_name", property = "caseName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "name_cn", property = "nameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_name", property = "companyName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "dept_name", property = "deptName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "key_words", property = "keyWords", jdbcType = JdbcType.VARCHAR),
            @Result(column = "details", property = "details", jdbcType = JdbcType.VARCHAR),
            @Result(column = "loop_close_remark", property = "loopCloseRemark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "loop_close_files", property = "loopCloseFiles", jdbcType = JdbcType.VARCHAR),
            @Result(column = "age_close_remark", property = "ageCloseRemark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "age_close_files", property = "ageCloseFiles", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select * from " + DB1TableName.PCBA_FILE + " where pcba_device_resource_id = #{pcbaDeviceResourceId}")
    List<PcbaFile> getPcbaFileByDeviceResourceId(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 根据状态查询
     *
     * @param status
     * @return
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_FILE + " where 1 = 1 " +
            "<if test='status != null and status != \"\" '>" +
            " and status = #{status}" +
            "</if>" +
            "<if test='pcbaCompanyId != null and pcbaCompanyId != \"\" '>" +
            " and pcba_company_id = #{pcbaCompanyId}" +
            "</if>" +
            "<if test='searchFileName != null and searchFileName != \"\" '>" +
            " and pcba_file_name like CONCAT('%',#{searchFileName},'%')" +
            "</if>" +
            "<if test=\"companyIdList != null and companyIdList.size() > 0 \">\n" +
            "                and pcba_company_id in\n" +
            "                <foreach item=\"item\" index=\"index\" collection=\"companyIdList\"\n" +
            "                         open=\"(\" separator=\",\" close=\")\">\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>\n" +
            " order by create_time desc" +
            "</script>")
    @ResultMap(value = "pcbaFileResultMap")
    List<PcbaFile> getAllPcbaFileByStatus(@Param("status") Integer status, @Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("searchFileName") String searchFileName, @Param("companyIdList") List<Integer> companyIdList);

    /**
     * 根据id查询
     *
     * @param pcbaFileId
     * @return
     */
    @ResultMap(value = "pcbaFileResultMap")
    @Select("select * from " + DB1TableName.PCBA_FILE + " where pcba_file_id = #{pcbaFileId}")
    PcbaFile getPcbaFileById(@Param("pcbaFileId") Integer pcbaFileId);

    /**
     * 根据 资源id查询不重复的分组名
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Select("select DISTINCT pcba_file_group from " + DB1TableName.PCBA_FILE + " where pcba_device_resource_id = #{pcbaDeviceResourceId} and pcba_file_group is not null and pcba_file_group != '' ")
    List<String> getPcbaFileGroupByResourceId(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId);

    /**
     * 新增
     *
     * @param pcbaFile
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_FILE + "(pcba_device_resource_id,pcba_file_type,pcba_file_name,pcba_file_path,status,create_time,file_group_id,creator_id,notes,pcba_company_id,submitter,auditors,case_name,name_cn,company_name,dept_name,key_words,details) values " +
            "(#{pcbaFile.pcbaDeviceResourceId},#{pcbaFile.pcbaFileType},#{pcbaFile.pcbaFileName},#{pcbaFile.pcbaFilePath},#{pcbaFile.status},#{pcbaFile.createTime},#{pcbaFile.fileGroupId},#{pcbaFile.creatorId},#{pcbaFile.notes},#{pcbaFile.pcbaCompanyId},#{pcbaFile.submitter},#{pcbaFile.auditors},#{pcbaFile.caseName},#{pcbaFile.nameCn},#{pcbaFile.companyName},#{pcbaFile.deptName},#{pcbaFile.keyWords},#{pcbaFile.details})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbaFile.pcbaFileId")
    Integer addPcbaFile(@Param("pcbaFile") PcbaFile pcbaFile);

    /**
     * 改变文件的状态
     *
     * @param status
     * @return
     */
    @Update("update " + DB1TableName.PCBA_FILE + " set status = #{status}, notes = #{notes}, submitter = #{submitter} where pcba_file_id = #{pcbaFileId}")
    Integer updatePcbaFileStatus(@Param("status") Integer status, @Param("notes") String notes, @Param("pcbaFileId") Integer pcbaFileId, @Param("submitter") Integer submitter);

    /**
     * 更新
     *
     * @param pcbaFile
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_FILE +
            "<set>" +
            "<if test='pcbaFile.pcbaFileName != null and pcbaFile.pcbaFileName != \"\" '>" +
            " pcba_file_name = #{pcbaFile.pcbaFileName}," +
            "</if>" +
            "<if test='pcbaFile.pcbaFilePath != null and pcbaFile.pcbaFilePath != \"\" '>" +
            " pcba_file_path = #{pcbaFile.pcbaFilePath}," +
            "</if>" +
            "<if test='pcbaFile.status != null and pcbaFile.status != \"\" '>" +
            " status = #{pcbaFile.status}," +
            "</if>" +
            "<if test='pcbaFile.fileGroupId != null and pcbaFile.fileGroupId != \"\" '>" +
            " file_group_id = #{pcbaFile.fileGroupId}," +
            "</if>" +
            "<if test='pcbaFile.creatorId != null and pcbaFile.creatorId != \"\" '>" +
            " creator_id = #{pcbaFile.creatorId}," +
            "</if>" +
            "<if test='pcbaFile.notes != null and pcbaFile.notes != \"\" '>" +
            " notes = #{pcbaFile.notes}," +
            "</if>" +
            "<if test='pcbaFile.submitter != null and pcbaFile.submitter != \"\" '>" +
            " submitter = #{pcbaFile.submitter}," +
            "</if>" +
            "<if test='pcbaFile.auditors != null and pcbaFile.auditors != \"\" '>" +
            " auditors = #{pcbaFile.auditors}," +
            "</if>" +
            "<if test='pcbaFile.caseName != null and pcbaFile.caseName != \"\" '>" +
            " case_name = #{pcbaFile.caseName}," +
            "</if>" +
            "<if test='pcbaFile.nameCn != null and pcbaFile.nameCn != \"\" '>" +
            " name_cn = #{pcbaFile.nameCn}," +
            "</if>" +
            "<if test='pcbaFile.companyName != null and pcbaFile.companyName != \"\" '>" +
            " company_name = #{pcbaFile.companyName}," +
            "</if>" +
            "<if test='pcbaFile.deptName != null and pcbaFile.deptName != \"\" '>" +
            " dept_name = #{pcbaFile.deptName}," +
            "</if>" +
            "<if test='pcbaFile.keyWords != null and pcbaFile.keyWords != \"\" '>" +
            " key_words = #{pcbaFile.keyWords}," +
            "</if>" +
            "<if test='pcbaFile.details != null and pcbaFile.details != \"\" '>" +
            " details = #{pcbaFile.details}," +
            "</if>" +
            "<if test='pcbaFile.pcbaDeviceResourceId != \"\" '>" +
            " pcba_device_resource_id = #{pcbaFile.pcbaDeviceResourceId}" +
            "</if>" +
            "</set>" +
            " where pcba_file_id = #{pcbaFile.pcbaFileId}" +
            "</script>")
    Integer updatePcbaFile(@Param("pcbaFile") PcbaFile pcbaFile);

    /**
     * 改变文件的关联设备
     *
     * @param pcbaDeviceResourceId
     * @return
     */
    @Update("update " + DB1TableName.PCBA_FILE + " set pcba_device_resource_id = #{pcbaDeviceResourceId},file_group_id = #{fileGroupId} where pcba_file_id = #{pcbaFileId}")
    Integer updatePcbaFileRelatedPcbaDeviceResource(@Param("pcbaDeviceResourceId") Integer pcbaDeviceResourceId, @Param("fileGroupId") Integer fileGroupId, @Param("pcbaFileId") Integer pcbaFileId);

    /**
     * 删除
     *
     * @param pcbaFileId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_FILE + " where pcba_file_id = #{pcbaFileId}")
    Integer deletePcbaFile(@Param("pcbaFileId") Integer pcbaFileId);

}
